package top.oylan.manager.service.impl;

import org.springframework.stereotype.Service;
import top.oylan.manager.entity.Comment;
import top.oylan.manager.entity.example.CommentExample;
import top.oylan.manager.entity.extend.CommentCondition;
import top.oylan.manager.mapper.CommentMapper;
import top.oylan.manager.service.CommentService;
import top.oylan.manager.tools.Pager;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author abduahad
 * @Description ${description}
 * @Date 2019-08-25
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    CommentMapper commentMapper;

    @Override
    public List<Comment> selectRecommend(Integer productId,Integer userId) {
        CommentExample example = getExample(productId, (byte) 1);
        example.setUserId(userId);
        return commentMapper.selectByExample(example);
    }

    @Override
    public List<Comment> selectByPager(Pager pager) {
        CommentCondition condition = (CommentCondition)pager.getCondition();
        CommentExample example = getExample(condition.getProductId(),(byte) 0);
        long count = commentMapper.countByExample(example);
        pager.setTotalRow((int)count);
        return commentMapper.selectByPager(pager);
    }

    private CommentExample getExample(Integer productId,byte recommend){
        CommentExample example = new CommentExample();
        CommentExample.Criteria criteria = example.createCriteria();
        criteria.andVisibleEqualTo((byte)1);
        criteria.andProductIdEqualTo(productId);
        if(recommend == 0){
            criteria.andRecommendEqualTo((byte)1);
        }
        return example;
    }
}
